package com.stork.sms.activity;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.navdrawer.SimpleSideDrawer;
import com.stork.sms.R;
import com.stork.sms.R.id;
import com.stork.sms.R.layout;
import com.stork.sms.R.menu;
import com.stork.sms.service.all.ServiceMethods;
import com.stork.sms.service.profile.StudentProfile;
import com.stork.sms.utilities.Constants;
import com.stork.sms.utilities.SharedPreference;

public class StudentProfileActivity extends Activity {
	private static final String SOAP_ACTION = "http://tempuri.org/StudentDEtails";
	private static final String METHOD_NAME = "StudentDEtails";
	private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:2341/smsservice/webmethods.asmx";
    public static final String LOGIN_ID="username";
	TextView firstname,lastname,dob,age,gender,yofadmission,jclass;
	TextView vfname,vlname,vdob,vage,vgender,vyofadmission,vjclass;
	ProgressDialog progressdialog;
	 Constants con;	
	 String sfname,slname,sdob,sage,sgender,syofa,sjclass;
	 JSONArray Items=null;
	    ArrayList<HashMap<String,Integer>>Itemlist;
	    StudentProfile studentProfile;
	    String datas;
	    private SimpleSideDrawer ssdrawer;
	   

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_student_profile);
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Profile");
		actionBar.setDisplayHomeAsUpEnabled(true);
		firstname=(TextView)findViewById(R.id.fname);
		lastname=(TextView)findViewById(R.id.lname);
		dob=(TextView)findViewById(R.id.dob);
		age=(TextView)findViewById(R.id.age);
		gender=(TextView)findViewById(R.id.gender);
		yofadmission=(TextView)findViewById(R.id.yofadmission);
		jclass=(TextView)findViewById(R.id.clas);
		vfname=(TextView)findViewById(R.id.fnameval);
		vlname=(TextView)findViewById(R.id.lnameval);
		vdob=(TextView)findViewById(R.id.dobval);
		vage=(TextView)findViewById(R.id.ageval);
		vgender=(TextView)findViewById(R.id.genderval);
		vyofadmission=(TextView)findViewById(R.id.yofadmival);
		vjclass=(TextView)findViewById(R.id.classval);
		ssdrawer=new SimpleSideDrawer(this);
		ssdrawer.setRightBehindContentView(R.layout.drawer);
		new Student().execute();
	
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	 public boolean onOptionsItemSelected(MenuItem item) {
	      switch (item.getItemId()) {
	         // Respond to the action bar's Up/Home button
	         case android.R.id.home:
	        	 super.onBackPressed();
	         return true;
	         case R.id.action_settings:
	 			ssdrawer.toggleRightDrawer();
	 			return true;
//	         case R.id.logout:
//	        	 Intent logout= new Intent(this, MainActivity.class);
//	        	 startActivity(logout);
//	        	 return true;
	         
	      }
	      return super.onOptionsItemSelected(item);
	   }
	private class Student extends AsyncTask<String, String,StudentProfile>
	{
		
		Toast toast = Toast.makeText(getApplicationContext(), "",
	            Toast.LENGTH_SHORT);
		
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		progressdialog=new ProgressDialog(StudentProfileActivity.this);
		progressdialog.setMessage("Please Wait......");
		progressdialog.setCancelable(false);
		progressdialog.show();
	}

		@Override
		protected StudentProfile doInBackground(String... params) {
			String username=SharedPreference.readString(getApplicationContext(), LOGIN_ID,"");
			ServiceMethods sm=new ServiceMethods();
		studentProfile=sm.profile(username);
			// TODO Auto-generated method stub
//		
			return studentProfile;
		}
			
			protected void onPostExecute(final StudentProfile studentProfile) {
				// TODO Auto-generated method stub
				//super.onPostExecute(result);
				if(progressdialog.isShowing())
					progressdialog.dismiss();
				String code=studentProfile.getCode();
				String msg=studentProfile.getMessage();
				System.out.println("value code is"+code);
				
			if(code.equalsIgnoreCase(con.mSERVICEINVALID)){	
//				Log.i("inside post","444 exception");
				
				StudentProfileActivity.this.runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							 final AlertDialog.Builder alertBox = new AlertDialog.Builder(StudentProfileActivity.this);
							// TODO Auto-generated method stub
							String msg=studentProfile.getMessage();
							
							toast.setText(msg);
							toast.show();
							toast.setText(msg);
							toast.show();
							alertBox.setTitle("Error Message");
							alertBox.setMessage(msg);
							alertBox.setCancelable(true);
							alertBox.setPositiveButton("Yes",
				                    new DialogInterface.OnClickListener() {
				                public void onClick(DialogInterface dialog, int id) {
				                    Intent A=new Intent(StudentProfileActivity.this,GridHomeMenuActivity.class);
				                    startActivity(A);
				                    finish();
				                    }
				                	
				            });
							AlertDialog ad=alertBox.create();
							ad.show();	
						}
						});
					
			}
			else if(code.equalsIgnoreCase(con.mSERVICEFAILED)){
				Log.i("inside post","404 exception");
				
				StudentProfileActivity.this.runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						 final AlertDialog.Builder alertBox = new AlertDialog.Builder(StudentProfileActivity.this);
						// TODO Auto-generated method stub
						String msg=studentProfile.getMessage();
						
						toast.setText(msg);
						toast.show();
						toast.setText(msg);
						toast.show();
						alertBox.setTitle("Error Message");
						alertBox.setMessage(msg);
						alertBox.setCancelable(true);
						alertBox.setPositiveButton("Yes",
			                    new DialogInterface.OnClickListener() {
			                public void onClick(DialogInterface dialog, int id) {
			                    Intent A=new Intent(StudentProfileActivity.this,GridHomeMenuActivity.class);
			                    startActivity(A);
			                    finish();
			                    }
			                	
			            });
						AlertDialog ad=alertBox.create();
						ad.show();	
					}
					});
	
			}
			else{
				System.out.println("on post \t"+studentProfile.getStudentDetails().getStudentFirstName());
				System.out.println("on post \t"+studentProfile.getStudentDetails().getStudentLastName());
				System.out.println("on post \t"+studentProfile.getStudentDetails().getStudentDob());
				System.out.println("validation code"+studentProfile.getCode());
				vfname.setText(studentProfile.getStudentDetails().getStudentFirstName()+"");
					vlname.setText(studentProfile.getStudentDetails().getStudentLastName()+"");
					vdob.setText(studentProfile.getStudentDetails().getStudentDob()+"");
					vage.setText(studentProfile.getStudentDetails().getStudentAge()+"");
					vgender.setText(studentProfile.getStudentDetails().getStudentGender()+"");
					vyofadmission.setText(studentProfile.getStudentDetails().getStudentYearOfAdmission()+"");
					vjclass.setText(studentProfile.getStudentDetails().getStudentClass()+"");
				}
			
		}
	}
}
